//
//  CHUploadDataController.m
//  CarFile
//
//  Created by 陈浩 on 2017/7/17.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHUploadDataController.h"
#import "CHUploadDataDetailController.h"
#import "UploadDataModel.h"
#import "CHUploadDataCell.h"
#import "CHJGSXViewController.h"
#import "CHScreeningViewController.h"
#import "CHSortViewController.h"
@interface CHUploadDataController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,assign) NSInteger year;
@property (nonatomic ,assign) NSInteger month;
@property (nonatomic ,copy) NSString *levels;
@property (nonatomic ,copy) NSString *areaid;

@end

@implementation CHUploadDataController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    
    return _datas;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"上传数据质量";
    self.view.backgroundColor =  KRGB(239, 239, 249);
    self.areaid = (NSString *)[Util getValue:@"areaid"];
    
    //     设置右侧导航栏按钮
    [self setupRightBarButtonItem];
    
    [self setupHeadView];
    
    [self setupTableView];
    
    [self setupDate];
    
    [BQActivityView showActiviTy];
    
    //    [self showLoading:YES];
    
    [self getData];
    
}

-(void)setupDate{
    
    NSDate *date = [NSDate date];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    _year = [components year];
    _month = [components month]-1;
    
}


-(void)setupRightBarButtonItem{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = CGRectMake(0, 0, 40 ,15);
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [btn setTitle:@"筛选" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(screeningData) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
}

//  筛选
-(void)screeningData{
    
    id obj = [LongriseRouter openURL:@"CHSortViewController"];
    if(obj && [obj isKindOfClass:[UIViewController class]])
    {
        UIViewController *vc = obj;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //判断该类是否可以传参数
    if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
    {
        id<LongriseRouterDelegater> delegater = obj;
        if(delegater)
        {
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            
            params[@"month"] = [NSNumber numberWithInteger:_month];
            params[@"year"] = [NSNumber numberWithInteger:_year];
            __weak typeof(self) weakSelf = self;
            //进行传值，传入统一的block以供B进行回调
            [delegater setParams:params block:^(id obj) {
                
                if(obj && [obj isKindOfClass:[NSMutableDictionary class]])
                {
                    
                    NSMutableDictionary *dict = obj;
                    weakSelf.areaid = dict[@"areaid"];
                    weakSelf.year = [dict[@"year"] integerValue];
                    weakSelf.month = [dict[@"month"] integerValue];
                    weakSelf.levels = dict[@"levels"];
                    [weakSelf getData];
                }
            }];
        }
    }
}




-(void)getData{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"areaid"] = self.areaid;
    params[@"sign"] = [DESCript encrypt:self.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"levels"] = self.levels;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"year"] = [NSString stringWithFormat:@"%zd",_year];
    params[@"month"] = [NSString stringWithFormat:@"%zd",_month];
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovdataqualitylist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        [self.datas removeAllObjects];
        
        [BQActivityView hideActiviTy];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                UploadDataModel *model = [UploadDataModel objectWithKeyValues:dict];
                [self.datas addObject:model];
            }
        }
        [self.tableView reloadData];
    }];
    
    
}


-(void)setupTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, UIScreenW, UIScreenH - 40 - 64)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = KRGB(239, 239, 249);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
}


-(void)setupHeadView{
    
    UILabel *areacnLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenW /2, 40)];
    areacnLabel.text = @"区域";
    areacnLabel.font = [UIFont systemFontOfSize:14];
    areacnLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:areacnLabel];
    
    UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(UIScreenW /2, 0, UIScreenW /2, 40)];
    scoreLabel.text = @"分数";
    scoreLabel.font = [UIFont systemFontOfSize:14];
    scoreLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:scoreLabel];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CHUploadDataCell *cell = [CHUploadDataCell cellForTableView:tableView];
    NSString *userType = (NSString *)[Util getValue:@"usertype"];
    if ([userType isEqualToString:@"99"]) {
        cell.userInteractionEnabled = NO;
    }
    if (indexPath.row == self.datas.count -1) {
        cell.userInteractionEnabled = NO;
    }
    cell.model = self.datas[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id obj = [LongriseRouter openURL:@"CHUploadDataDetailController"];
    if(obj && [obj isKindOfClass:[UIViewController class]])
    {
        UIViewController *vc = obj;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //判断该类是否可以传参数
    if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
    {
        id<LongriseRouterDelegater> delegater = obj;
        if(delegater)
        {
            
            UploadDataModel *model = self.datas[indexPath.row];
            
            //进行传值，传入统一的block以供B进行回调
            [delegater setParams:model block:^(id obj) {
                
                
            }];
        }
    }
}

#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
        
        NSMutableDictionary *dict = obj;
        self.index = [dict[@"index"] integerValue];
        
    }
}

@end
